<template>
	<view>
		<!-- 搜索框 -->
		<view class="search">
			<uni-search-bar  @input="input" :radius="100" cancelButton="none" clearButton="auto"></uni-search-bar>
		</view>
		<!-- 搜索列表 -->
		<view class="sugg-list" v-if="searchList.length !=0 ">
			<view class="sugg--list-item" v-for="(item,index) in searchList" :key="index" @click="gotoGoodsList(item)">
				<text>{{item.goods_name}}</text>
				<uni-icons type="forward" size="17"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in historys" :key="index" class="history-list-item" type="primary"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value:'',
				// 搜索列表
				searchList:[],
				//搜索历史
				historyList:[]
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('value') || '[]')
		},
		methods:{
			// input输入事件的处理函数
			input(e){
				clearTimeout(this.timer);
				// console.log(e)
			this.timer = setTimeout(()=>{
				this.value = e;
				this.getSearchList()
			},500)

			},
			// 获取搜索列表
			async getSearchList(){
				if(this.value == ''){
					this.searchList=[];
					return uni.$showMsg()
				}
				const {data:res} = await uni.$http.get('api/public/v1/goods/qsearch',{query:this.value})
			if(res.meta.status !== 200){
				return uni.$showMsg();
			}
			this.searchList = res.message;
			this.saveSearchHistory()
			},
			//跳转到商品详情页
			gotoGoodsList(item){
				uni.navigateTo({
					url:"../goods_list/goods_list?goods_id=" +item.goods_id 
				})
			},
			// 将搜索后的关键词放入搜索历史数组中
			saveSearchHistory(){
				// this.historyList.push(this.value)
				const set = new Set(this.historyList);
				set.delete(this.value);
				set.add(this.value);
				// console.log(set)
			this.historyList = Array.from(set);
			// 对搜索历史数据进行永久保存
			uni.setStorageSync('value',JSON.stringify(this.historyList))
			},
			// 清空搜索历史
			clean(){
				this.historyList = [];
				uni.getStorageSync('value',[])
			}
		},
		computed:{
			// 对搜索历史数据进行排序
			historys(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search{
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list{
	width: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	padding: 0 5px;
}
.sugg--list-item{
	width: 100%;
	padding: 10px 0;
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: 1px solid #ccc;
	text{
		white-space: nowrap;
		overflow: hidden;
		// 溢出部分用...显示
		text-overflow: ellipsis;
		font-size: 12px;
	}	
}
.history-title{
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: 1px solid #ccc;
	padding: 10px 5px;
	text{
		font-size: 14px;
	}
}
.history-list{
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	.history-list-item{
		margin-top: 5px;
		margin-right: 5px;
	}
}
</style>
